package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.EmployeeBasicInfo;
import com.ruoyi.system.mapper.WorkExperienceMapper;
import com.ruoyi.system.domain.WorkExperience;
import com.ruoyi.system.service.IWorkExperienceService;

/**
 * 工作经历Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-22
 */
@Service
public class WorkExperienceServiceImpl implements IWorkExperienceService 
{
    @Autowired
    private WorkExperienceMapper workExperienceMapper;

    /**
     * 查询工作经历
     * 
     * @param id 工作经历主键
     * @return 工作经历
     */
    @Override
    public WorkExperience selectWorkExperienceById(String id)
    {
        return workExperienceMapper.selectWorkExperienceById(id);
    }

    /**
     * 查询工作经历列表
     * 
     * @param workExperience 工作经历
     * @return 工作经历
     */
    @Override
    public List<WorkExperience> selectWorkExperienceList(WorkExperience workExperience)
    {
        return workExperienceMapper.selectWorkExperienceList(workExperience);
    }

    /**
     * 新增工作经历
     * 
     * @param workExperience 工作经历
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWorkExperience(WorkExperience workExperience)
    {
        int rows = workExperienceMapper.insertWorkExperience(workExperience);
        insertEmployeeBasicInfo(workExperience);
        return rows;
    }

    /**
     * 修改工作经历
     * 
     * @param workExperience 工作经历
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWorkExperience(WorkExperience workExperience)
    {
        workExperienceMapper.deleteEmployeeBasicInfoByEmployeeId(workExperience.getId());
        insertEmployeeBasicInfo(workExperience);
        return workExperienceMapper.updateWorkExperience(workExperience);
    }

    /**
     * 批量删除工作经历
     * 
     * @param ids 需要删除的工作经历主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkExperienceByIds(String[] ids)
    {
        workExperienceMapper.deleteEmployeeBasicInfoByEmployeeIds(ids);
        return workExperienceMapper.deleteWorkExperienceByIds(ids);
    }

    /**
     * 删除工作经历信息
     * 
     * @param id 工作经历主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkExperienceById(String id)
    {
        workExperienceMapper.deleteEmployeeBasicInfoByEmployeeId(id);
        return workExperienceMapper.deleteWorkExperienceById(id);
    }

    /**
     * 新增员工基本信息信息
     * 
     * @param workExperience 工作经历对象
     */
    public void insertEmployeeBasicInfo(WorkExperience workExperience)
    {
        List<EmployeeBasicInfo> employeeBasicInfoList = workExperience.getEmployeeBasicInfoList();
        String id = workExperience.getId();
        if (StringUtils.isNotNull(employeeBasicInfoList))
        {
            List<EmployeeBasicInfo> list = new ArrayList<EmployeeBasicInfo>();
            for (EmployeeBasicInfo employeeBasicInfo : employeeBasicInfoList)
            {
                employeeBasicInfo.setEmployeeId(id);
                list.add(employeeBasicInfo);
            }
            if (list.size() > 0)
            {
                workExperienceMapper.batchEmployeeBasicInfo(list);
            }
        }
    }
}
